package com.lonty.vote.service;

import com.lonty.vote.api.Result;
import com.lonty.vote.api.ResultCode;
import com.lonty.vote.mapper.master.VoteContentMapper;
import com.lonty.vote.mapper.master.VoteRecordsMapper;
import com.lonty.vote.model.User;
import com.lonty.vote.model.VoteRecords;
import com.lonty.vote.request.VoteList;
import com.lonty.vote.request.VoteTodayList;
import com.lonty.vote.util.WhiteListUtil;
import com.lonty.vote.vo.VoteRecordsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/5/25 10:16
 * @Version 1.0
 */
@Service
public class VoteRecordsService {

    @Resource
    private VoteRecordsMapper voteRecordsMapper;

    @Resource
    private UserService userService;

    @Resource
    private VoteContentMapper voteContentMapper;

    /**
     * 投票
     * @param voteRecordsVo
     * @return
     */
    @Transactional
    public Result vote(VoteRecordsVo voteRecordsVo){
        //获取用户
        User user = userService.login(voteRecordsVo.getToken());
        //判断是否是白名单成员
        if(WhiteListUtil.isContains(user.getId())){
            return Result.builder(102, "您已被取消参赛资格").build();
        }
        //先判断当前用户今天是否投过票
        List<VoteRecords> voteRecordsList = voteRecordsMapper.getUserTodayRecords(user.getId());
        if(voteRecordsList.size() > 0){
            return Result.builder(ResultCode.VOTED).build();
        }
        int result = 0;
        //根据逗号分割投票内容ID
        String voteContentId[] = voteRecordsVo.getVoteContentIds().split(",");
        for(int i = 0;i< voteContentId.length;i++){
            if("0".equals(voteContentId[i])){
                return Result.builder(101, "请正确投票").build();
            }
            VoteRecords voteRecords = new VoteRecords();
            voteRecords.setUserId(user.getId());
            voteRecords.setVoteContentId(voteContentId[i]);
            voteRecords.setCreateTime(LocalDate.now());
            voteRecords.setIsBackups(0);
            result += voteRecordsMapper.saveVoteRecords(voteRecords);
        }
        if(result > 0){
            return Result.builder().build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    /**
     * 统计当天票数
     * @return
     */
    public List<VoteTodayList> getVoteTodayList(){
        List<VoteTodayList> list = voteRecordsMapper.getVoteTodayList();
        return list;
    }

    /**
     * 统计所有投票总数
     * @return
     */
    @Transactional
    public List<VoteList> getAllList(){

        List<VoteList> voteListList = new ArrayList<>();
        for(int i = 1;i <= 24;i++){
            VoteList voteList = new VoteList();
            voteList.setName(voteContentMapper.getContentById(i));
            List<Integer> nums = new ArrayList<>();
            for(int j = 1;j <= 8;j++){
                Integer num = 0;
                if(j != 8){
                    num = voteRecordsMapper.getVoteModel(i, "2020-06-0" + j);
                }else{
                    num = voteRecordsMapper.getVoteModel(i, "");
                }
                if(num == null){
                    nums.add(0);
                }else{
                    nums.add(num);
                }
                voteList.setNums(nums);
            }
            voteListList.add(voteList);
        }

        return voteListList;
    }

}
